# -*- coding: UTF-8 -*-

from Utiles.SendMethod import SendMethod
from Interface.UserInterface.LogInterface import LoginInterface
from Utiles.GetKeywords import GetKeywords
from Utiles.TimeFormat import get_now_time


class SalesInterface(object):

    def __init__(self, headers):
        self.url = "http://192.168.10.150"
        self.headers = headers

    def build_number(self):
        url = self.url + "/jshERP-boot/sequence/buildNumber"
        method = "get"
        res =  SendMethod.send_method(url=url, method=method, headers=self.headers)
        num = GetKeywords.get_keyword(res, "defaultNumber")
        return num

    def find_link_number(self):
        url = self.url + "/jshERP-boot/depotHead/list?" + "search=%7B%22number%22:%22%22,%22materialParam%22:%22%22,%22type%22:%22%E5%85%B6%E5%AE%83%22,%22subType%22:%22%E9%94%80%E5%94%AE%E8%AE%A2%E5%8D%95%22,%22status%22:%221,3%22%7D&column=createTime&order=desc&field=id,,organName,number,materialsList,operTimeStr,userName,materialCount,totalPrice,totalTaxLastMoney,status&currentPage=1&pageSize=10"
        method = "get"
        res = SendMethod.send_method(url=url, method=method, headers=self.headers)
        num = GetKeywords.get_keyword(res, "defaultNumber")
        return num

    def add_sales_order(self):
        url = self.url + "/jshERP-boot/depotHead/addDepotHeadAndDetail"
        method = "post"
        payload = {"info":"{\"defaultNumber\":\"XSDD00000000826\","
                          "\"organId\":58,\"operTime\":\"2025-03-23 14:33:16\","
                          "\"number\":\"XSDD00000000686\",\"discount\":0,"
                          "\"discountMoney\":0,\"discountLastMoney\":0,"
                          "\"accountId\":17,\"changeAmount\":0,\"type\":\"其它\","
                          "\"subType\":\"销售订单\",\"totalPrice\":0,\"accountIdList\":\"\","
                          "\"accountMoneyList\":\"\","
                          "\"fileName\":\"\",\"salesMan\":\"\",\"status\":\"1\"}",
                   "rows":"[{\"id\":\"17427115964180288629\",\"hiddenKey\":\"\","
                          "\"name\":\"办公椅\",\"standard\":\"一个\",\"model\":null,"
                          "\"color\":null,\"materialOther\":\"\",\"stock\":1000,"
                          "\"unit\":\"个\",\"sku\":\"\",\"operNumber\":1,"
                          "\"unitPrice\":null,\"allPrice\":null,\"taxRate\":0,"
                          "\"taxMoney\":0,\"taxLastMoney\":null,\"remark\":\"\","
                          "\"barCode\":\"1019\",\"depotId\":\"\"}]"}
        info = eval(payload["info"])#取出info，修改为字典
        num = self.build_number()
        info["defaultNumber"] = "XSDD" + str(num)
        info["number"] = "XSDD" + str(num)
        info['operTime'] = get_now_time()
        payload["info"] = str(info)
        return SendMethod.send_method(url=url, method=method, json=payload, headers=self.headers)

    def add_sales_out(self):
        url = self.url + "/jshERP-boot/depotHead/addDepotHeadAndDetail"
        method = "post"
        payload = {"info":"{\"defaultNumber\":\"XSCK00000000690\",\"organId\":58,\"operTime\":\"2025-03-23 15:05:59\",\"number\":\"XSCK00000000690\",\"discount\":0,\"discountMoney\":0,\"discountLastMoney\":0,\"otherMoney\":0,\"accountId\":17,\"changeAmount\":0,\"debt\":0,\"type\":\"出库\",\"subType\":\"销售\",\"totalPrice\":0,\"accountIdList\":\"\",\"accountMoneyList\":\"\",\"fileName\":\"\",\"salesMan\":\"\",\"status\":\"1\"}","rows":"[{\"id\":\"17427135593220211261\",\"name\":\"小米剃须刀\",\"standard\":null,\"model\":null,\"color\":null,\"materialOther\":\"\",\"stock\":1000,\"unit\":\"个\",\"snList\":\"\",\"batchNumber\":\"\",\"expirationDate\":\"\",\"sku\":\"\",\"preNumber\":\"\",\"finishNumber\":\"\",\"operNumber\":1,\"unitPrice\":null,\"allPrice\":null,\"taxRate\":0,\"taxMoney\":0,\"taxLastMoney\":null,\"remark\":\"\",\"linkId\":\"\",\"depotId\":\"14\",\"barCode\":\"1020\"}]"}
        info = eval(payload["info"])  # 取出info，修改为字典
        num = self.build_number()
        info["defaultNumber"] = "XSDD" + str(num)
        info["number"] = "XSCK" + str(num)
        info['operTime'] = get_now_time()
        info["linkNumber"] = self.find_link_number()
        payload["info"] = str(info)
        return SendMethod.send_method(url=url, method=method, json=payload, headers=self.headers)


if __name__ == '__main__':

    res = LoginInterface().tenant_login("jsh", "123456")
    token = GetKeywords.get_keyword(res, "token")
    headers = {"x-access-token": token}
    sales = SalesInterface(headers)
    print(sales.add_sales_out())